

from astropy import units as u
from astropy.time import Time
from netorchestr.envir.base import ONet
from netorchestr.envir.mobility import Trajectory
from netorchestr.envir.node.uav import UavServerBase
from netorchestr.envir.node.ue import UeBase
from netorchestr.envir.node.controller import ControllerGlobal4Ue
from netorchestr.envir.physicallayer import RadioMedium

if __name__ == '__main__':
    net = ONet('UavServerSfcs')
    
    init_time = Time("2021-07-22 00:00:00")

    vim_1 = UavServerBase('Vim1', init_time, [112.80, 37.45, 4000]) # 中国山西省忻州市附近
    vim_1_trajectory = Trajectory(name='V1Tr', avg_speed=(25200*u.km/u.h).to(u.m/u.s).value)
    vim_1_trajectory.add_figure8(center=vim_1.mobiusTraj.init_gps, size=(100*u.km).to(u.m).value, loops=20)
    vim_1.mobiusTraj.set_trajectory(vim_1_trajectory, init_time)
    vim_1.switch.networkLayer.ip_addr = '192.168.1.1'
    vim_1.duAau.networkLayer.ip_addr = '192.168.1.10'         # eth0
    vim_1.vnfList[0].networkLayer.ip_addr = '192.168.1.11'    # eth1
    vim_1.switch.networkLayer.routing_table['192.168.1.11'] = vim_1.switch.ethernetPhyList[1].name
    vim_1.switch.networkLayer.routing_table['192.168.2.11'] = vim_1.switch.ethernetPhyList[0].name
    net.add_module(vim_1)
    
    
    vim_2 = UavServerBase('Vim2', init_time, [116.65, 40.64, 4000]) # 中国河北省承德市附近
    vim_2_trajectory = Trajectory(name='V2Tr', avg_speed=(25200*u.km/u.h).to(u.m/u.s).value)
    vim_2_trajectory.add_circle(center=vim_2.mobiusTraj.init_gps, radius=(100*u.km).to(u.m).value, loops=20)
    vim_2.mobiusTraj.set_trajectory(vim_2_trajectory, init_time)
    vim_2.switch.networkLayer.ip_addr = '192.168.2.1'
    vim_2.duAau.networkLayer.ip_addr = '192.168.2.10'         # eth0
    vim_2.vnfList[0].networkLayer.ip_addr = '192.168.2.11'    # eth1
    vim_2.switch.networkLayer.routing_table['192.168.2.11'] = vim_2.switch.ethernetPhyList[1].name
    vim_2.switch.networkLayer.routing_table['192.168.3.11'] = vim_2.switch.ethernetPhyList[0].name
    vim_2.switch.networkLayer.routing_table['192.168.0.4']  = vim_2.switch.ethernetPhyList[0].name
    net.add_module(vim_2)
    
    
    vim_3 = UavServerBase('Vim3', init_time, [120.50, 43.83, 4000]) # 中国内蒙古自治区赤峰市附近
    vim_3_trajectory = Trajectory(name='V3Tr', avg_speed=(25200*u.km/u.h).to(u.m/u.s).value)
    vim_3_trajectory.add_figure8(center=vim_3.mobiusTraj.init_gps, size=(100*u.km).to(u.m).value, loops=20)
    vim_3.mobiusTraj.set_trajectory(vim_3_trajectory, init_time)
    vim_3.switch.networkLayer.ip_addr = '192.168.3.1'
    vim_3.duAau.networkLayer.ip_addr = '192.168.3.10'         # eth0
    vim_3.vnfList[0].networkLayer.ip_addr = '192.168.3.11'    # eth1
    vim_3.switch.networkLayer.routing_table['192.168.3.11'] = vim_3.switch.ethernetPhyList[1].name
    vim_3.switch.networkLayer.routing_table['192.168.0.2']  = vim_3.switch.ethernetPhyList[0].name
    net.add_module(vim_3)
    
    
    ue_1 = UeBase(name='Ue1', init_time=init_time, init_gps=[112.80, 37.45, 0.0])
    ue_1_trajectory = Trajectory(name='U1Tr', avg_speed=1.5)
    ue_1_trajectory.add_random_in_circle(center=ue_1.mobiusTraj.init_gps, radius=(10*u.km).to(u.m).value, anchor_number=10)
    ue_1.mobiusTraj.set_trajectory(ue_1_trajectory, init_time)
    ue_1.networkLayer.ip_addr = '192.168.0.1'
    # ue_1.appLayer.clocktime.stop()
    net.add_module(ue_1)
    
    
    ue_2 = UeBase(name='Ue2', init_time=init_time, init_gps=[120.50, 43.83, 0.0])
    ue_2_trajectory = Trajectory(name='U2Tr', avg_speed=1.5)
    ue_2_trajectory.add_random_in_circle(center=ue_2.mobiusTraj.init_gps, radius=(10*u.km).to(u.m).value, anchor_number=10)
    ue_2.mobiusTraj.set_trajectory(ue_2_trajectory, init_time)
    ue_2.networkLayer.ip_addr = '192.168.0.2'
    ue_2.appLayer.clocktime.stop()
    net.add_module(ue_2)
    
    
    ue_3 = UeBase(name='Ue3', init_time=init_time, init_gps=[112.80, 37.45, 0.0])
    ue_3_trajectory = Trajectory(name='U3Tr', avg_speed=1.5)
    ue_3_trajectory.add_random_in_circle(center=ue_3.mobiusTraj.init_gps, radius=(10*u.km).to(u.m).value, anchor_number=10)
    ue_3.mobiusTraj.set_trajectory(ue_3_trajectory, init_time)
    ue_3.networkLayer.ip_addr = '192.168.0.3'
    # ue_3.appLayer.clocktime.stop()
    net.add_module(ue_3)
    
    
    ue_4 = UeBase(name='Ue4', init_time=init_time, init_gps=[116.65, 40.64, 0.0])
    ue_4_trajectory = Trajectory(name='U4Tr', avg_speed=1.5)
    ue_4_trajectory.add_random_in_circle(center=ue_4.mobiusTraj.init_gps, radius=(10*u.km).to(u.m).value, anchor_number=10)
    ue_4.mobiusTraj.set_trajectory(ue_4_trajectory, init_time)
    ue_4.networkLayer.ip_addr = '192.168.0.4'
    ue_4.appLayer.clocktime.stop()
    net.add_module(ue_4)
    
    radio_medium = RadioMedium('RadioMedium')
    net.add_module(radio_medium)
    net.ready_for_medium(radio_medium)
    
    controller_global = ControllerGlobal4Ue('Controller', net)
    net.add_module(controller_global)
    
    net.draw_flag = True
    net.draw_time_accuracy = 1*u.ms
    net.draw_region = [112.80-0.5, 
                       120.50+0.5, 
                       37.45-0.5, 
                       43.83+0.5]
    net.run(until=100*u.ms)
    # net.run(until=0.5*u.hour)
    
    controller_global.draw_results()

